//
// Created by summerday on 2021/6/5.
//

#include "iostream"
#include "queue.h"
using namespace std;

void initQueue(LinkQueue &q){
    q.front = q.rear = (LinkQueueNode*) malloc(sizeof (LinkQueueNode));
    q.front->next = NULL;
}

void enq(LinkQueue &q, int x){
    LinkQueueNode *s = (LinkQueueNode*) malloc(sizeof (LinkQueueNode));
    s->data = x;
    s->next = NULL;
    q.rear->next = s;
    q.rear = s;
}

bool deq(LinkQueue &q, int &x){
    if(empty(q)) {
        return false;
    }
    LinkQueueNode *p = q.front->next;
    x = p->data;
    q.front->next = p->next;
    // last one
    if(q.rear == p){
        q.rear = q.front;
    }
    free(p);
    return true;
}

// 指到一起 初始化 or size == 0
bool empty(LinkQueue q){
    // or q.front->next == NULL
    return q.front == q.rear;
}

bool full(LinkQueue q){
    // 牺牲一个格子 or size == MAX_SIZE
    return q.rear->next = q.front;
}

bool getHead(LinkQueue q, int &x){
    if(empty(q)){
        return false;
    }
    x = q.front->next->data;
    return true;
}

